import { login, logout, getInfo, getSystem } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
	state: {
		token: getToken(),
		name: '',
		avatar: '',
		roles: [],
		permissions: [],
		dept: [],
		info: {},
		isLogin: false,
		selectType: 1, //1单选 2 多选 3他人日程,
		selectedUser: [],
		disabledUser: [],
		business: {},
		day: null,
		userIndex: {}
	},
	mutations: {
		SET_TOKEN(state, res) {
			state.token = res
		},
		setUserIndex(state, res) {
			state.userIndex = res
		},
		selectUser(state, res) {
			state.selectType = res.type || 1
			state.selectedUser = res.selectedUser || []
			state.disabledUser = res.disabledUser || []
		},
		setDay(state, res) {
			state.day = res
		},
		setBusiness(state, res) {
			state.business = res
		},
		setSelectedUser(state, res) {
			state.disabledUser = []
			state.selectedUser = res
		},
		setSelectType(state, res) {
			state.selectType = res
		},
		setSelectKey(state, res) {
			state.selectKey = res
		},
		SET_NAME: (state, name) => {
			state.name = name
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
		},
		SET_DEPT: (state, dept) => {
			state.dept = dept
		},
		SET_INFO: (state, info) => {
			uni.setStorageSync("userInfo", info)
			state.info = info
		},
		SET_LOGIN: (state, res) => {
			state.isLogin = res
		},
	},

	actions: {
		// 登录
		Login({ commit }, userInfo) {
			return new Promise((resolve, reject) => {
				login(userInfo).then(res => {
					setToken(res.data.token.token)
					commit('SET_TOKEN', res.data.token.token)
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({ commit, state }) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					commit('SET_INFO', res.data || {})
					if (res.data.account) {
						commit('SET_LOGIN', true)
					} else {
						commit('SET_LOGIN', false)
					}
					resolve(res)
				}).catch(error => {
					commit('SET_INFO', {})
					commit('SET_LOGIN', false)
					reject(error)
				})
			})
		},

		GetSystem({ commit, state }) {
			return new Promise((resolve, reject) => {
				getSystem().then(res => {

					uni.setStorageSync('phone', res.data.phone);
					uni.setStorageSync('attorneyPath', res.data.attorneyPath);
					uni.setStorageSync('paymentPath', res.data.paymentPath);
					uni.setStorageSync('agreementPath', res.data.agreementPath);
					uni.setStorageSync('signaturePath', res.data.signaturePath);

					resolve(res)
				}).catch(error => {

					reject(error)
				})
			})
		},



		// 退出系统
		LogOut({ commit, state }) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_TOKEN', '')
					commit('SET_ROLES', [])
					commit('SET_INFO', {})
					commit('SET_PERMISSIONS', [])
					commit('SET_LOGIN', false)
					removeToken()
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}

export default user